
import pymysql

class MyDb():
    def __init__(self):
        self.db=pymysql.connect(
            user='root',
            password='',
            host='localhost',
            database='test',
            port=3306
        )
        self.cursor=self.db.cursor()

    def execteSQL(self,sql):
        self.cursor.execute(sql)
        self.db.commit()

    def query_one(self,sql):
        self.cursor.execute(sql)
        res=self.cursor.fetchone()
        return res

    def query_many(self,sql):
        self.cursor.execute(sql)
        res1=self.cursor.fetchmany(2)
        return res1
    def query_all(self,sql):
        self.cursor.execute(sql)
        res2=self.cursor.fetchall()
        return res2
mydb=MyDb()

if __name__=="__main__":
    #新增
    # sql_insert = "insert into dept(deptno,dname,loc) values(891,'人没事','红河市');"
    # mydb.execteSQL(sql_insert)
    # #删除
    # sql_del = "delete from dept where deptno=1151;"
    # mydb.execteSQL(sql_del)
    #查询
    sql_sel='select user_id from ecs_users where user_name="你是大傻逼";'
    res=mydb.query_one(sql_sel)
    print(res)
    user_id =res[0]
    print(user_id)

    sql="select order_id from ecs_order_info where user_id=6;"
    res2 = mydb.query_one(sql)
    print(res2)
    order_id = res2[0]
    print(order_id)

    del1="delete from ecs_order_info where order_id= %s;"%user_id
    mydb.execteSQL(del1)
    del2="delete from ecs_order_info where user_id= %s;"%user_id
    mydb.execteSQL(del2)